home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 98 / Skunkware 98.iso / src / mail / pine3.96.tar.gz / pine3.96.tar / pine3.96 / imap / non-ANSI / c-client / nntp.c < prev    next >
C/C++ Source or Header  |  1996-06-28  |  6KB  |  163 lines

  1. /*
  2.  * Program:    Network News Transfer Protocol (NNTP) routines
  3.  *
  4.  * Author:    Mark Crispin
  5.  *        Networks and Distributed Computing
  6.  *        Computing & Communications
  7.  *        University of Washington
  8.  *        Administration Building, AG-44
  9.  *        Seattle, WA  98195
  10.  *        Internet: MRC@CAC.Washington.EDU
  11.  *
  12.  * Date:    10 February 1992
  13.  * Last Edited:    28 June 1996
  14.  *
  15.  * Copyright 1996 by the University of Washington
  16.  *
  17.  *  Permission to use, copy, modify, and distribute this software and its
  18.  * documentation for any purpose and without fee is hereby granted, provided
  19.  * that the above copyright notices appear in all copies and that both the
  20.  * above copyright notices and this permission notice appear in supporting
  21.  * documentation, and that the name of the University of Washington not be
  22.  * used in advertising or publicity pertaining to distribution of the software
  23.  * without specific, written prior permission.  This software is made
  24.  * available "as is", and
  25.  * THE UNIVERSITY OF WASHINGTON DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED,
  26.  * WITH REGARD TO THIS SOFTWARE, INCLUDING WITHOUT LIMITATION ALL IMPLIED
  27.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, AND IN
  28.  * NO EVENT SHALL THE UNIVERSITY OF WASHINGTON BE LIABLE FOR ANY SPECIAL,
  29.  * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
  30.  * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, TORT
  31.  * (INCLUDING NEGLIGENCE) OR STRICT LIABILITY, ARISING OUT OF OR IN
  32.  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  33.  *
  34.  */
  35.  
  36.  
  37. #include <ctype.h>
  38. #include <stdio.h>
  39. #include "mail.h"
  40. #include "osdep.h"
  41. #include "smtp.h"
  42. #include "nntp.h"
  43. #include "rfc822.h"
  44. #include "misc.h"
  45.  
  46.  
  47. /* Mailer parameters */
  48.  
  49. long nntp_port = 0;        /* default port override */
  50.  
  51. /* Network News Transfer Protocol open connection
  52.  * Accepts: service host list
  53.  *        initial debugging flag
  54.  * Returns: T on success, NIL on failure
  55.  */
  56.  
  57. SMTPSTREAM *nntp_open (hostlist,debug)
  58.     char **hostlist;
  59.     long debug;
  60. {
  61.   SMTPSTREAM *stream = NIL;
  62.   long i;
  63.   void *tcpstream;
  64.   if (!(hostlist && *hostlist)) mm_log ("Missing NNTP service host",ERROR);
  65.   else do {            /* try to open connection */
  66.     if (tcpstream = nntp_port ? tcp_open (*hostlist,NIL,nntp_port) :
  67.     tcp_open (*hostlist,"nntp",NNTPTCPPORT)) {
  68.       stream = (SMTPSTREAM *) fs_get (sizeof (SMTPSTREAM));
  69.       stream->tcpstream = tcpstream;
  70.       stream->debug = (debug & OP_DEBUG) ? T : NIL;
  71.       stream->reply = NIL;
  72.       i = smtp_reply (stream);    /* get server greeting */
  73.       if (debug & OP_READONLY) {/* if just want to read, either code is OK */
  74.     if ((i == NNTPGREET) || (i == NNTPGREETNOPOST))
  75.       mm_log (stream->reply + 4,(long) NIL);
  76.     else {            /* oops */
  77.       mm_log (stream->reply,ERROR);
  78.       stream = smtp_close (stream);
  79.     }
  80.       }
  81.       else if (i != NNTPGREET) {/* want to post */
  82.     mm_log (stream->reply,ERROR);
  83.     stream = smtp_close (stream);
  84.       }
  85.     }
  86.   } while (!stream && *++hostlist);
  87.                 /* some silly servers require this */
  88.   if (stream) nntp_send (stream,"MODE","READER");
  89.   return stream;
  90. }
  91.  
  92. /* Network News Transfer Protocol deliver news
  93.  * Accepts: stream
  94.  *        message envelope
  95.  *        message body
  96.  * Returns: T on success, NIL on failure
  97.  */
  98.  
  99. long nntp_mail (stream,env,body)
  100.     SMTPSTREAM *stream;
  101.     ENVELOPE *env;
  102.     BODY *body;
  103. {
  104.   long ret = NIL;
  105.   char tmp[8*MAILTMPLEN],*s;
  106.                 /* negotiate post command */
  107.   if (nntp_send (stream,"POST",NIL) == NNTPREADY) {
  108.                 /* set up error in case failure */
  109.     smtp_fake (stream,SMTPSOFTFATAL,"NNTP connection went away!");
  110.     /* Gabba gabba hey, we need some brain damage to send netnews!!!
  111.      *
  112.      * First, we give ourselves a frontal lobotomy, and put in some UUCP
  113.      *  syntax.  It doesn't matter that it's completely bogus UUCP, and
  114.      *  that UUCP has nothing to do with anything we're doing.
  115.      *
  116.      * Then, we bop ourselves on the head with a ball-peen hammer.  How
  117.      *  dare we be so presumptious as to insert a *comment* in a Date:
  118.      *  header line.  Why, we were actually trying to be nice to a human
  119.      *  by giving a symbolic timezone (such as PST) in addition to a
  120.      *  numeric timezone (such as -0800).  But the gods of news transport
  121.      *  will have none of this.  Unix weenies, tried and true, rule!!!
  122.      */
  123.                 /* RFC-1036 requires this cretinism */
  124.     sprintf (tmp,"Path: %s!%s\015\012",tcp_localhost (stream->tcpstream),
  125.          env->from ? env->from->mailbox : "foo");
  126.                 /* here's another cretinism */
  127.     if (s = strstr (env->date," (")) *s = NIL;
  128.                 /* output data, return success status */
  129.     ret = tcp_soutr (stream->tcpstream,tmp) &&
  130.       (*(rfc822emit_t) mail_parameters (NIL,GET_RFC822OUTPUT,NIL))
  131.     (tmp,env,body,smtp_soutr,stream->tcpstream) &&
  132.       (nntp_send (stream,".",NIL) == NNTPOK);
  133.     if (s) *s = ' ';        /* put the comment in the date back */
  134.   }
  135.   return ret;
  136. }
  137.  
  138. /* NNTP send command
  139.  * Accepts: SEND stream
  140.  *        text
  141.  * Returns: reply code
  142.  */
  143.  
  144. long nntp_send (stream,command,args)
  145.     SMTPSTREAM *stream;
  146.     char *command;
  147.     char *args;
  148. {
  149.   char tmp[MAILTMPLEN],usr[MAILTMPLEN],pwd[MAILTMPLEN];
  150.   long ret = smtp_send (stream,command,args);
  151.   if ((ret == NNTPWANTAUTH) || (ret == NNTPWANTAUTH2)) {
  152.     sprintf (tmp,"{%s/nntp}",tcp_host (stream->tcpstream));
  153.                 /* get user name and password */
  154.     mm_login (tmp,usr,pwd,(long) 1);
  155.                 /* try it if user gave one */
  156.     if (*pwd && (nntp_send (stream,"AUTHINFO USER",usr) == NNTPWANTPASS) &&
  157.     (nntp_send (stream,"AUTHINFO PASS",pwd) == NNTPAUTHED))
  158.                 /* resend the command */
  159.       ret = nntp_send (stream,command,args);
  160.   }
  161.   return ret;
  162. }
  163.